{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": "'\\n1.在一个单因子试验中,因子A有三个水平,每个水平下各重复4次,具体数据如下:\\n    水平                      数据\\n    一水平     8       5       7        4\\n    二水平     6       10      12       9\\n    三水平     0       1       5        2\\n    试计算误差平方和Se,因子A的平方和Sa,总平方和St,并指出它们各自的自由度.\\n    2.在一个单因子试验中,因子A有4个水平,每个水平下重复次数分别为5,7,6,8.那么误差平方和、A的平方和及总平方和的自由度各是多少?\\n    3.在单因子试验中,因子A有4个水平,每个水平下各重复3次试验,\\n    现已求得每个水平下试验结果的样本标准差分别为1.5,2.0,1.6,1.2,则其误差平方和为多少﹖误差的方差o的估计值是多少?\\n    4.在单因子方差分析中,因子A有三个水平,每个水平各做4次重复试验,请完成下列方差分析表,并在显著性水平α=0.05下对因子A是否显著作出检验.\\n'"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import statsmodels.api as sm\n",
    "from statsmodels.formula.api import ols\n",
    "\"\"\"\n",
    "1.在一个单因子试验中,因子A有三个水平,每个水平下各重复4次,具体数据如下:\n",
    "    水平                      数据\n",
    "    一水平     8       5       7        4\n",
    "    二水平     6       10      12       9\n",
    "    三水平     0       1       5        2\n",
    "    试计算误差平方和Se,因子A的平方和Sa,总平方和St,并指出它们各自的自由度.\n",
    "    2.在一个单因子试验中,因子A有4个水平,每个水平下重复次数分别为5,7,6,8.那么误差平方和、A的平方和及总平方和的自由度各是多少?\n",
    "    3.在单因子试验中,因子A有4个水平,每个水平下各重复3次试验,\n",
    "    现已求得每个水平下试验结果的样本标准差分别为1.5,2.0,1.6,1.2,则其误差平方和为多少﹖误差的方差o的估计值是多少?\n",
    "    4.在单因子方差分析中,因子A有三个水平,每个水平各做4次重复试验,请完成下列方差分析表,并在显著性水平α=0.05下对因子A是否显著作出检验.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "   1   2   3  4\n0  8   5   7  4\n1  6  10  12  9\n2  0   1   5  2",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>8</td>\n      <td>5</td>\n      <td>7</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>6</td>\n      <td>10</td>\n      <td>12</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>1</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#第一题\n",
    "data = pd.read_excel('E:\\Project\\Python\\Statistics\\Third\\danyinzi.xlsx')\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24 37 8\n"
     ]
    },
    {
     "data": {
      "text/plain": "69"
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r = 3 #因子水平数量\n",
    "m = 4 #重复次数\n",
    "n = r*m # 总实验\n",
    "T1,T2,T3 = 0,0,0\n",
    "\n",
    "for i in range(4): #  次数\n",
    "   T1 =T1 +int(data.iloc[0,i])\n",
    "\n",
    "   T2 = T2+int(data.iloc[1,i])\n",
    "   T3 = T3+int(data.iloc[2,i])\n",
    "print(T1,T2,T3)\n",
    "T = T1+T2+T3\n",
    "T"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "154 361 30\n",
      "10.0 18.75 14.0\n"
     ]
    }
   ],
   "source": [
    "#误差平方和\n",
    "TT1,TT2,TT3 = 0,0,0\n",
    "for i in range(4):\n",
    "    TT1 = TT1+int(data.iloc[0,i])**2\n",
    "    TT2 = TT2+int(data.iloc[1,i])**2\n",
    "    TT3 = TT3+int(data.iloc[2,i])**2\n",
    "print(TT1,TT2,TT3)\n",
    "Se1 = TT1-(T1**2)/4\n",
    "Se2 = TT2-(T2**2)/4\n",
    "Se3 = TT3-(T3**2)/4\n",
    "print(Se1,Se2,Se3)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "data": {
      "text/plain": "42.75"
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fe = 9\n",
    "Se = Se3 + Se2 +Se1\n",
    "Se"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "105.5"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Sa = (T1**2 +T2**2+T3**2)/4 - (T**2)/12\n",
    "Sa"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [],
   "source": [
    "St = (TT1+TT2+TT3)-(T**2)/12\n",
    "St"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "误差平方和S、因子A的平方和Sa与总平方和St分别为:105.5和148.25\n"
     ]
    }
   ],
   "source": [
    "print(\"误差平方和S、因子A的平方和Sa与总平方和St分别为:{}和{}\".format(Sa,St))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "误差平方和的自由度:22,因子A的平方和的自由度:3,总平方和的自由度25\n"
     ]
    }
   ],
   "source": [
    "# 第二题\n",
    "r = 4 #因子水平数\n",
    "n = 5+6+7+8 # 总实验次数\n",
    "Fe = n-r # 误差平方和的自由度\n",
    "Fa = r -1 # 因子A的平方和的自由度\n",
    "FT =n-1 #总平方和的自由度\n",
    "print(\"误差平方和的自由度:{},因子A的平方和的自由度:{},总平方和的自由度{}\".format(Fe,Fa,FT))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "outputs": [
    {
     "data": {
      "text/plain": "2.5625"
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SE1 = 2*1.5**2\n",
    "SE2 = 2*2**2\n",
    "SE3 = 2 *1.6**2\n",
    "SE4 = 2*1.2**2\n",
    "SE = SE1+SE2+SE3+SE4\n",
    "FE = 3*4-4\n",
    "sigma2 = SE/FE\n",
    "sigma2"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}